cmake_minimum_required(VERSION 3.16)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_C_STANDARD 11)
#set(QT_VERSION 6.2.4)
set(QT_VERSION 5.15.2)
string(REPLACE "." ";" VERSION_LIST ${QT_VERSION})
list(GET VERSION_LIST 0 QT_VERSION_MAJOR)

IF (WIN32)
    add_compile_options("$<$<C_COMPILER_ID:MSVC>:/utf-8>")
    add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>")
    add_compile_definitions(WIN32_LEAN_AND_MEAN)
    if (EXISTS "D:/ProgramFiles/Qt/${QT_VERSION}")
        set(QT_PATH "D:/ProgramFiles/Qt/${QT_VERSION}/msvc2019_64")
    elseif (EXISTS "C:/Program Files/Qt/${QT_VERSION}")
        set(QT_PATH "C:/Program Files/Qt/${QT_VERSION}/msvc2019_64")
    endif ()
ELSE ()
    if (EXISTS "/home/linux/Qt/")
        set(QT_PATH "/home/linux/Qt/${QT_VERSION}/gcc_64")
    endif ()
ENDIF ()
set(CMAKE_PREFIX_PATH ${QT_PATH})

project(ModbusTool VERSION 0.1 LANGUAGES CXX)

set(CMAKE_AUTOMOC ON)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if (${QT_VERSION_MAJOR} EQUAL 5)
    find_package(Qt5 REQUIRED COMPONENTS Quick SerialPort Network QuickControls2)
    set(QT_LIB Qt5::Quick Qt5::SerialPort Qt5::Network Qt5::QuickControls2)
    set(QML_QRC qml.qrc)
    set(CMAKE_AUTOUIC ON)
    set(CMAKE_AUTORCC ON)
elseif (${QT_VERSION_MAJOR} EQUAL 6)
    find_package(Qt6 REQUIRED COMPONENTS Quick SerialPort Network)
    set(QT_LIB Qt6::Quick Qt6::SerialPort Qt6::Network Qt6::QuickControls2)
endif ()



add_executable(MGModbusTool
    ${QML_QRC}
    main.cpp
    mgapp.cpp
)

if (${QT_VERSION_MAJOR} EQUAL 6)
qt_add_qml_module(appModbusTool
    URI view
    VERSION 1.0
    QML_FILES Main.qml
)
endif ()

# 去除调试信息
set_target_properties(MGModbusTool PROPERTIES
    MACOSX_BUNDLE_GUI_IDENTIFIER mgkjgw.top
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    MACOSX_BUNDLE TRUE
    WIN32_EXECUTABLE TRUE
)

target_link_libraries(MGModbusTool
    PRIVATE ${QT_LIB}
)

install(TARGETS MGModbusTool
    BUNDLE DESTINATION .
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
